// 类(Classes)

export {}

class Person {
    // ES2017中新增
    name: string// = 'init name'
    age: number// = 11


    constructor(name: string, age: number) {
        this.name = name
        this.age = age 
    }

    sayHi(msg: string): void {
        console.log(`I am ${this.name}, ${msg}`)
    }
}

// ==================================================
// 访问修饰符
class PersonSync {
    // ES2017中新增
    public name: string// = 'init name' 公有成员 默认 建议加 都可以访问 
    private age: number// = 11  私有属性 只能类的内部访问 不能在外部和子类中访问
    protected gender: boolean //受保护的 只能在子类中访问

    // protected constructor(name: string, age: number) { //protected constructor 不允许实例化 允许继承
    constructor(name: string, age: number) { //protected constructor 不允许实例化 允许继承
        this.name = name
        this.age = age 
        this.gender = true
    }

    sayHi(msg: string): void {
        console.log(`I am ${this.name}, ${msg}`)
    }
}

// protected constructor  不允许实例化
const tom = new PersonSync('tom', 18)
console.log(tom.name)
// 属性“age”为私有属性，只能在类“PersonSync”中访问。
// console.log(tom.age)

// 属性“gender”受保护，只能在类“PersonSync”及其子类中访问。
// console.log(tom.gender)


// constructor  允许继承
class Student extends PersonSync {
    private constructor(name: string, age: number) {// private constructor 不能被实例化 不能被继承
        super(name, age)
        console.log(this.gender, 'gender111')
    }
    static create(name: string, age: number){
        return new Student(name, age)
    }
}

// constructor 不允许实例化
// const student = new Student('liuchao', 10)
// private constructor
const student = Student.create('liuchao', 10)

























